package com.smi.service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yesiming on 2017/6/11.
 */
public class GenUtil {

    private static Map<String, String> columnMap = new HashMap<String, String>();
    static {
        columnMap.put("varchar", "private String ");
        columnMap.put("int", "private long ");
        columnMap.put("datetime", "private LocalDateTime ");
        columnMap.put("date", "private LocalDateTime ");
        columnMap.put("tinyint", "private int ");
        columnMap.put("text", "private String ");
        columnMap.put("decimal", "private BigDecimal ");
    }

    /**
     * 生成bean的属性
     * @param columnType
     * @param columnName
     * @return
     */
    public static String convertField(String columnType, String columnName) {

        int indexOfBracket = 0;
        // 1. 处理enum类型
        if(columnType.startsWith("enum")) {
            return getEnumType(columnType, columnName);
        }
        // 2. 数值类型，varchar类型
        if((indexOfBracket = columnType.indexOf('(')) >= 0) {
            columnType = columnType.substring(0, indexOfBracket);
        }
        if(columnMap.get(columnType) != null) {
            return columnMap.get(columnType) + columnName + ";";
        } else {
            return "private UN_DEF " + columnName + ";";
        }
    }

    public static String convertSql(String columnType, String columnName) {

        int indexOfBracket = 0;
        // 1. 处理enum类型
        if(columnType.startsWith("enum")) {
            return getEnumType(columnType, columnName);
        }
        // 2. 数值类型，varchar类型
        if((indexOfBracket = columnType.indexOf('(')) >= 0) {
            columnType = columnType.substring(0, indexOfBracket);
        }
        if(columnMap.get(columnType) != null) {
            return columnMap.get(columnType) + columnName + ";";
        } else {
            return "private UN_DEF " + columnName + ";";
        }
    }

    /**
     * 取得数值类型长度
     * @param columnType
     * @return
     */
    public static int getSize (String columnType) {
        String size = columnType.substring(columnType.indexOf('(') + 1, columnType.indexOf(')'));
        return Integer.parseInt(size);
    }

    /**
     * enum('AAA','AA','A','BBB','BB','B')
      */

    public static String getEnumType(String columnType, String columnName) {
        String [] arr = columnName.split("_");
        StringBuffer sb = new StringBuffer("private ");
        if(arr != null && arr.length > 0) {

            for(String str : arr) {
                sb.append(str.substring(0, 1).toUpperCase());
                sb.append(str.substring(1));
            }
        } else {
            sb.append(columnName.substring(0, 1).toUpperCase());
            sb.append(columnName.substring(1));
        }
        sb.append("Type ");
        sb.append(columnName);
        sb.append("; //");
        sb.append(columnType);
        return sb.toString();
    }

    /**
     * 替换下划线，首字母大写
     * @param original
     * @return
     */
    public static String replaceUnderLineWithFirstLetter(String original) {
        String [] arr = original.split("_");
        StringBuffer sb = new StringBuffer("");
        if(arr != null && arr.length > 0) {

            for(String str : arr) {
                sb.append(str.substring(0, 1).toUpperCase());
                sb.append(str.substring(1));
            }
        } else {
            sb.append(original.substring(0, 1).toUpperCase());
            sb.append(original.substring(1));
        }
        return sb.toString();
    }

    /**
     * 替换下划线，不包括首字母
     * @param original
     * @return
     */
    public static String replaceUnderLineWithOutFirstLetter(String original) {
        String [] arr = original.split("_");
        StringBuffer sb = new StringBuffer("");
        if(arr != null && arr.length > 0) {
            sb.append(arr[0]);
            for(int i = 1; i < arr.length; i++) {
                sb.append(arr[i].substring(0, 1).toUpperCase());
                sb.append(arr[i].substring(1));
            }
        }
        return sb.toString();
    }

    public static void main(String [] args) {
        //String str = getEnumType("enum('AAA','AA','A','BBB','BB','B')", "comp_a_b");
        String str = replaceUnderLineWithOutFirstLetter("hello_world_en");
        System.out.println(str);
    }

}
